package com.aacar.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Repository;

import com.aacar.constant.ConsFiled;
import com.aacar.constant.RedisConsField;
import com.aacar.service.TokenService;
import com.aacar.utils.RedisCacheManagerUtil;
import com.aacar.utils.SpringContextUtil;
import com.aacar.utils.TokenUtil;
import com.aacar.vo.TokenVo;

@Repository("tokenService")
public class TokenServiceImpl implements TokenService {
	
	
	@Autowired  
    private RedisTemplate<String,String> redisTemplate;
	
	//从redis中获取token
	public TokenVo retrieveToken() {
		TokenVo tv = new TokenVo();
		//1.生成token
		String token = TokenUtil.generatorToken();
		tv.setToken(token);
		return tv;
	}
	
	//校验token
	public String validatorToken(String token) {
		ValueOperations<String, String> valueOper = redisTemplate.opsForValue(); 
		Object value = valueOper.get(RedisConsField.pre_login+token);
		if(null != value) {
			return value.toString();
		}
		return null;
	}

	
	//移除token
	public TokenVo removeToken(String token) {
		return null;
	}


	@Override
	public TokenVo generateToken() {
		TokenVo tv = new TokenVo();
		//1.生成token
		String token = TokenUtil.generatorToken();
		tv.setToken(token);
		return tv;
	}
	
	

}
